package sudoku;



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
//import com.jgoodies.forms.factories.DefaultComponentFactory;




public class Sudoku extends JApplet {
	private JTable table;
	
	private JTable grille;
	Object[][] donnees =null;
	

	public static void main(String[] args) {
		new Sudoku();
	}
	/**
	 * Create the applet.
	 */
	public Sudoku() {
		
		
		
		getContentPane().setBackground(Color.WHITE);
		getContentPane().setLayout(new BorderLayout(0, 0));
	
		//grille = new JTable(new MonModele(9,9,));
	
		//gril.setGrilleCourante(gtmp);
		
		grille = new JTable(9,9);
		
		
		JTable table_1 = new JTable();
		table_1.setBorder(UIManager.getBorder("EditorPane.border"));
		getContentPane().add(table_1, BorderLayout.CENTER);
		
		
		
		
		Font fontMenu = new Font("Verdana",Font.BOLD,12);
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnGrille = new JMenu("Grille");
		mnGrille.setFont(new Font("Segoe UI", Font.PLAIN, 12));
		menuBar.add(mnGrille);
		
		JMenu mnNouvelleGrille = new JMenu("Nouvelle grille");
		mnGrille.add(mnNouvelleGrille);
		
		JMenuItem mntmFacile = new JMenuItem("Facile", new ImageIcon(Sudoku.class.getClassLoader().getResource("images/mini_gen.png")));
		mntmFacile.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {  
																											grille.setModel(new MonModele(9,9,1));
																											genererGrille(1);}});
		mnNouvelleGrille.add(mntmFacile);

		JMenuItem mntmMoyen = new JMenuItem("Moyen");
		mntmMoyen.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {   
																											grille.setModel(new MonModele(9,9,2));
																											genererGrille(2);}});
		mnNouvelleGrille.add(mntmMoyen);

		JMenuItem mntmDifficile = new JMenuItem("Difficile");
		mntmDifficile.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {  
																												grille.setModel(new MonModele(9,9,3));
																												 genererGrille(3);}});
		mnNouvelleGrille.add(mntmDifficile);

		JMenuItem mntmDiabolique = new JMenuItem("Diabolique");
		mntmDiabolique.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {  
																												grille.setModel(new MonModele(9,9,4));
																												genererGrille(4);}});
		mnNouvelleGrille.add(mntmDiabolique);
		
		JMenu mnJeuEnReseau = new JMenu("Reseau");
		menuBar.add(mnJeuEnReseau);
		
		JMenuItem mntmClient = new JMenuItem("Client");
		mnJeuEnReseau.add(mntmClient);
		
		JMenuItem mntmServeur = new JMenuItem("Serveur");
		mnJeuEnReseau.add(mntmServeur);
		
		JMenu mnAide = new JMenu("Aide");
		menuBar.add(mnAide);
		
		JMenuItem mntmJeuEnRseau = new JMenuItem("Jeu en reseau");
		mnAide.add(mntmJeuEnRseau);
		
		JMenuItem mntmRgles = new JMenuItem("Regles ");
		mnAide.add(mntmRgles);
		
	
		
	}
	GrilleSudoku gril = null;
	public void genererGrille(int diff){
	
		donnees =  new Object[9][9];
		
		
		
		System.out.println(diff);
		gril = new GrilleSudoku(9, 'v', diff,false);
		int[][] gtmp = gril.generer(diff, false);
		grille.repaint();
		
		
		
		for (int i=0; i<9;i++){
			for (int j=0; j<9;j++){
				donnees[i][j]= new Case(gtmp[i][j]);
				if(gtmp[i][j]==-1){
					grille.setValueAt(null, i, j);
				}
				else{
				grille.setValueAt(gtmp[i][j], i, j);
				
				}
				
			}
		}
		
		grille.setBorder(new LineBorder(Color.black, 2));
		gril.afficher(gtmp);
		grille.setDefaultRenderer(Object.class, new MonCellRenderer());
		
		grille.repaint();
		
		

		// -1 quand la case est vide, donc condition, si machin = -1, alors rien dans la case
		
		
		
	}
	
	
	
	
	public class MonCellRenderer extends DefaultTableCellRenderer {

			
		
		public Component getTableCellRendererComponent(JTable table, Object value,
				boolean isSelected, boolean hasFocus, int row, int column) {
			Component cell = super.getTableCellRendererComponent(grille, value,
					isSelected, hasFocus, row, column);
		
			
		
			for(int k=0;k<9;k++){
				if (row == 3 && column == k || row == 6 && column == k)
					//cell.setBackground(Color.BLUE);
		               setBorder(new MatteBorder(2, 0, 0, 0, Color.black));
				} 
			
			grille.repaint();
			
			for(int i=0;i<9;i++){
				if (row == i && column == 3 || row == i && column == 6)
					//cell.setBackground(Color.BLUE);
		               setBorder(new MatteBorder(0, 2, 0, 0, Color.black));
				}
			
			if (row == 3 && column == 3 || row == 3 && column == 6 || row ==6 && column ==3 || row ==6 && column ==6){
				setBorder(new MatteBorder(2, 2, 0, 0, Color.black));
			}
			
			
			
	
			/////////////////////////////////////////////////////////////////////
			//ATTENTION, CELA VA COLORIER TOUTES LES CELLULES REMPLIES!!!//
			if (value != null) { 
	              cell.setBackground(Color.yellow);
	              
	          }
	          else {
	              
	                   cell.setBackground(Color.white);
	              }      

	        /////////////////////////////////////////////////////////////////////////  
			grille.repaint();
			return cell;
		}
	}
	
		
	
	public class MonModele extends AbstractTableModel{
		int row =0;
		int column =0;
		
		int diff=0;
		
		public MonModele(int row,int column, int diff){
			this.row=row;
			this.column=column;
			
			this.diff=diff;
		}
		@Override
		public int getColumnCount() {
			// TODO Auto-generated method stub
			return this.column;
		}

		@Override
		public int getRowCount() {
			// TODO Auto-generated method stub
			return this.row;
		}

		@Override
		public Object getValueAt(int arg0, int arg1) {
			// TODO Auto-generated method stub
			return donnees[arg0][arg1];
		}
		
		public boolean isCellEditable(int row, int col) {
			if (gril.positionEnoncer[row][col] ) {
				return false;
			} else {
				return true;
			}
		}
		
		public void setValueAt(Object value, int row, int col){
			
			if(row<9 && column<9)
			donnees[row][column]=value;
			
		}
		
		
		
		
	}
	
	public class Case{
	
		
		public Integer number;
		
		Case(int num)
		{
			number = new Integer(num);			
		}
		
		public String ToString(){
		 
		    return number.toString();
		}
		
		
	}
	
}



